package parser;

import antlr.RecognitionException;
import java.util.*;

class FromStmt extends Stmt {

    Expr module_name;
    List<NamePair> pairs = new ArrayList();
    int name;
    int as_name;
    
    public FromStmt(Expr module_name, int name, int as_name) { 
        this.module_name = module_name;
        this.name = name;
        this.as_name = as_name;
    }
    
    void addNames(int name, int as_name) {
        NamePair pair = new NamePair();
        pair.name = name;
        pair.as_name = as_name;
        pairs.add(pair);
    }

    void write(ByteStream out) throws RecognitionException {
        module_name.write(out);
        out.write(Opcodes.IMPORT);
        for (NamePair p : pairs) {
            out.write(Opcodes.COPY);   
            out.write(Opcodes.LOAD_ATTR);
            out.writeShort(p.name);
            out.write(Opcodes.STORE_GLOBAL);
            out.writeShort(p.as_name);
        }
        out.write(Opcodes.LOAD_ATTR);
        out.writeShort(name);
        out.write(Opcodes.STORE_GLOBAL);
        out.writeShort(as_name);
    }

}


class NamePair {
    int name;
    int as_name;
}
